// 存放文章相关接口
const express = require('express');
const router = express.Router();

const db = require('../db');

// ------------------------------- 添加文章接口 -----------------------------
const multer = require('multer');
const upload = multer({ dest: 'uploads' }); // 配置上传目录

// router.post('/add', 函数, 函数);

router.post('/add', upload.single('cover_img'), (req, res) => {
  // console.log(req.body);
  let { title, content, cate_id, state } = req.body;
  // console.log(req.file);
  let filename = req.file.filename;
  // 组织数据，添加到数据库即可
  db(`insert into article set title='${title}', cate_id=${cate_id},content='${content}',cover_img='${filename}',state='${state}',pub_date='2021-09-10 12:10:09', author_id=${req.user.id}`, (err, result) => {
    if (err) throw err;
    res.send({
      status: 0,
      message: '添加文章成功'
    })
  })
});

module.exports = router;
